package cn.tedu.cloudcar01.controller;

import cn.tedu.cloudcar01.base.response.JsonResult;
import cn.tedu.cloudcar01.base.response.StateCode;
import cn.tedu.cloudcar01.mapper.VehicleMapper;
import cn.tedu.cloudcar01.pojo.dto.VehicleAddParam;
import cn.tedu.cloudcar01.pojo.dto.VehicleSelectQuery;
import cn.tedu.cloudcar01.pojo.entiry.Vehicle;
import cn.tedu.cloudcar01.pojo.vo.UserQuery;
import cn.tedu.cloudcar01.pojo.vo.VehicleListQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
@Api(tags = "02.车辆管理")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    //添加车辆
    //需要验证用户登录信息
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        UserQuery  user = (UserQuery)session.getAttribute("user");
        if (user==null){
            return new JsonResult(StateCode.NOT_LOGIN);
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(user.getId());
        vehicleMapper.addVehicle(vehicle);
        return JsonResult.ok();
    }
    //车辆列表
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult list(VehicleSelectQuery vehicleSelectQuery, @ApiIgnore HttpSession session){
        UserQuery userQuery =(UserQuery)session.getAttribute("user");
        if (userQuery==null){
            return new JsonResult(StateCode.NOT_LOGIN);
        }
        String brand = vehicleSelectQuery.getBrand();
        String license = vehicleSelectQuery.getLicense();
        Long userId = userQuery.getId();
        List<VehicleListQuery> vehicleListQueries = vehicleMapper.list(brand,license,userId);
        return JsonResult.ok(vehicleListQueries);
        //删除车辆/*也要先校验登录状态
        // 根据车辆id来进行*/
    }
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @PostMapping("del/{id}")
    public JsonResult deleteVehicle(@Range(min = 1,max = 1000,message = "车辆的编号必须在1-1000之间") @PathVariable Integer id,@ApiIgnore HttpSession session){
        UserQuery userQuery=(UserQuery) session.getAttribute("user");
        if (userQuery==null){
            return new JsonResult(StateCode.NOT_LOGIN);
        }
        Long userId = userQuery.getId();
        vehicleMapper.deleteVehicleById(id,userId);
        return JsonResult.ok();
    }
}


























